#region "TypeExtensions"

/*
 * ####     Developer Name : Wang Yucai
 * 
 * ####     Development Tool : Microsoft VisualStudio 2010 Ultimate Edition
 * 
 * ####     Create Time : 2014-07-08 15:49:40
 * 
 * ####     Namespace : Masterduner.SourceStudio.Practices.DevelopmentLibrary
 * 
 * ####     Type Name : TypeExtensions
 * 
 * ####     Full Name : Masterduner.SourceStudio.Practices.DevelopmentLibrary.TypeExtensions
 * 
 * ####     Machine Name : GLCHQWYCWINW7
 * 
 * ####     UserName : GUOCOLAND/wangyucai
 * 
 * ####     CLR Version : 4.0.30319.18444
 * 
 * ####     Target Framework Version : 4.0
 */

#endregion

using System;

namespace Masterduner.SourceStudio.Practices.DevelopmentLibrary
{
    /// <summary>
    /// <para>Masterduner.SourceStudio.Practices.DevelopmentLibrary.TypeExtensions</para>
    /// <para>
    /// 为<see cref="Type"/>提供的扩展方法。
    /// </para>
    /// </summary>
    /// <remarks>
    /// <para>Target Framework Version : 4.0</para>
    /// </remarks>
    public static class TypeExtensions
    {
        #region InheritFrom
        /// <summary>
        /// 验证<paramref name="childType"/>是否继承自<paramref name="baseType"/>。
        /// </summary>
        /// <param name="childType"><see cref="Type"/>对象实例。</param>
        /// <param name="baseType"><see cref="Type"/>对象实例。</param>
        /// <returns>如果<paramref name="childType"/>继承自<paramref name="baseType"/>则返回true；否则返回false。</returns>
        static private bool InheritFrom(Type childType, Type baseType)
        {
            bool isInherited = childType.Equals(baseType);
            if (!isInherited && childType.BaseType.NotIsNull())
                isInherited = InheritFrom(childType.BaseType, baseType);
            return isInherited;
        }
        #endregion

        #region InheritFrom
        /// <summary>
        /// 验证<paramref name="current"/>是否继承自<typeparamref name="TBase"/>类型。
        /// </summary>
        /// <typeparam name="TBase">指定的基类类型。</typeparam>
        /// <param name="current"><see cref="Type"/>对象实例。</param>
        /// <returns>如果<paramref name="current"/>继承自<typeparamref name="TBase"/>则返回true；否则返回false。</returns>
        static public bool InheritFrom<TBase>(this Type current)
        {
            Type baseType = typeof(TBase);
            if (baseType.Equals(typeof(object)))
            {
                return true;
            }
            else
            {
                return InheritFrom(current, baseType);
            }
        }
        #endregion

        #region ImplementInterface
        /// <summary>
        /// 验证<paramref name="type"/>是否实现了<paramref name="interfaceType"/>类型的接口。
        /// </summary>
        /// <param name="type"><see cref="Type"/>对象实例。</param>
        /// <param name="interfaceType">接口类型。</param>
        /// <returns>如果<paramref name="type"/>实现了<paramref name="interfaceType"/>类型的接口则返回true；否则返回false。</returns>
        static private bool ImplementInterface(Type type, Type interfaceType)
        {
            Type[] interfaceTypes = type.GetInterfaces();
            bool isImplement = false;
            if (interfaceTypes.NotIsNull() && interfaceTypes.Length > 0)
            {
                foreach (Type item in interfaceTypes)
                {
                    isImplement = item.Equals(interfaceType);
                    if (isImplement) break;
                }
            }
            if (!isImplement && type.BaseType.NotIsNull()) isImplement = ImplementInterface(type.BaseType, interfaceType);
            return isImplement;
        }
        #endregion

        #region ImplementInterface
        /// <summary>
        /// 验证<paramref name="current"/>是否实现了<typeparamref name="TInterface"/>类型的接口。
        /// </summary>
        /// <typeparam name="TInterface">指定的接口类型。</typeparam>
        /// <param name="current"><see cref="Type"/>对象实例。</param>
        /// <returns>如果<paramref name="current"/>实现了<typeparamref name="TInterface"/>类型的接口则返回true；否则返回false。</returns>
        static public bool ImplementInterface<TInterface>(this Type current)
        {
            return ImplementInterface(current, TExpando<TInterface>.GetType());
        }
        #endregion
    }
}

#region README

/*
 * ####     ©2014 Wang Yucai
 * 
 * ####     Contact me
 *                  
 *                  E-mail : Masterduner@Yeah.net
 *                  Mobile : 13621384677
 *                  QQ : 180261899
 */

#endregion